﻿ 
  var ddlCityId = "selCity";
  var ddlAreaId = "selArea";
  var ddlCity = null;
  var ddlArea = null;
  $(function() {
      ddlCity = $("#selCity")[0];
      ddlArea = $("#selArea")[0];
  });
  var citys = $.evalJSON(citystr);
  $(function() {
      var tbCity = $(".tbCity");
      var tbArea = $(".tbArea");
      var city = tbCity.val();
      var area = tbArea.val();

      var exist = false;
      var cityModel = null;
      var areaModel = null;
      var gz = null;

      $(citys).each(function() {
          if (this.Name == "广州市") {
              gz = this;
              return false;
          }
      });
      $(citys).each(function() {
          if ((this.Id + "") == city) {
              exist = true;
              cityModel = this;
              return false;
          }
      });

      if (!exist) {
          $(gz.Childs).each(function() {
              if ((this.Id + "") == city) {
                  cityModel = this;
                  $(this.Childs).each(function() {
                      if ((this.Id + "") == area) {
                          areaModel = this;
                          return false;
                      }
                  });
                  return false;
              }

          });

          $("#areagz")[0].checked = true;
          areaTypeChange($("input[name=areaType]")[0]);


      } else {
          $("#areagd")[0].checked = true;
          areaTypeChange($("input[name=areaType]")[1]);
      }


      if (cityModel != null) {
          ddlCity.value = cityModel.Id;
          bindArea(cityModel);
          tbCity.val(cityModel.Id);
      }
      if (areaModel != null) {
          $("#" + ddlAreaId)[0].value = areaModel.Id;
          tbArea.val(areaModel.Id);
      }




      $("#areagz,#areagd").click(function() {
//          if (this.id == "areagz")
//              $("#spSQ").show();
//          else
//              $("#spSQ").hide();


          if (this.id == "areagz") {

              $(".spArea").html("商圈");
              $(".spCity").html("区域");

          }
          else {
              $(".spArea").html("区域");
              $(".spCity").html("城市"); 
          }


          areaTypeChange(this);

      });
      $(ddlCity).change(function() {
          bindArea(this.options[this.selectedIndex].tag);
          tbCity.val(this.value);
          tbArea.val("");
      });
      $(ddlArea).change(function() {
          tbArea.val(this.value);
      });
  });
  function areaTypeChange(obj) {

      if (ddlCity.areaType != obj) {

          if ($("#areagz")[0].checked) {
              bindCity("gz");
          } else {
              bindCity("gd");
          }
          ddlCity.areaType = obj;

          bindArea(ddlCity.options[ddlCity.selectedIndex].tag);
          $(".tbCity").val(ddlCity.value);
          $(".tbArea").val("");


      }
  }

  function bindCity(flag) {
      var options = ddlCity.options;
      options.length = 0;
      $(citys).each(function() {
          if (flag == "gz" && this.Name == "广州市") {

              $(this.Childs).each(function() {
                  var option = new Option(this.Name, this.Id);
                  option.tag = this;
                  options.add(option);
              });
              if (this.Childs.length > 0) {
                  bindArea(this.Childs[0]);
              }
          }
          if (flag == "gd" && this.Name != "广州市") {
              var option = new Option(this.Name, this.Id);
              option.tag = this;
              options.add(option);
          }
      });
  }
  function bindArea(city) {
      var options = ddlArea.options;
      options.length = 0;
      options.add(new Option("请选择", null));
      $(city.Childs).each(function() {
          var option = new Option(this.Name, this.Id);
          option.tag = this;
          options.add(option);
      });
  }
  function setCityValue() {
  }
  function setAreaValue() {
  }
